package com.chapter14.Map_;

import java.util.Comparator;
import java.util.TreeMap;

/**
 * @author 宋鑫宇
 * @version 1.0
 * 排序
 */
public class TreeMap_ {
    public static void main(String[] args) {
//        TreeMap treeMap = new TreeMap();
        //使用默认的构造器时的排序还是无序的
        TreeMap treeMap = new TreeMap(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
//                return ((String)o1).compareTo((String) o2);
                //按长度排序
                return ((String)o1).length() - ((String)o2).length();
            }
        });
        treeMap.put("12",123);
        treeMap.put("333",345);
        treeMap.put("1",234);
        System.out.println(treeMap);
    }
}
